// Learn TypeScript:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html



import AudioManage from "./AudioManage";

const {ccclass, property} = cc._decorator;

export enum voicePicStatus {
    openStatus = 0,
    closeStatus = 1
}

export enum vibrationPicStatus {
    openStatus = 3,
    closeStatus = 4
}

let voicePicUrl = ["icon_k","icon_g"];
let vibrationPicUrl = ["icon_k","icon_g"];
let mainUrl = "img/ui/";

let androidVibratorUrl = "org/cocos2dx/javascript/SDKManager/VibrationConfig";

@ccclass
export default class settingMediator{

    static vibrationStatus : boolean = true;

    static voiceStatus : boolean = true;

    //声音设置
    static voiceSwitch(){
        if(settingMediator.voiceStatus){
            // this.setPic(voicePicStatus.closeStatus,target);
            AudioManage.stopAll();
        }else{
            // this.setPic(voicePicStatus.openStatus,target);
            AudioManage.replayAll();
        }
        this.voiceStatus = !this.voiceStatus;
    }

    //震动设置
    static vibrationSwitch(){
        if(settingMediator.vibrationStatus){
            // this.setPic(vibrationPicStatus.closeStatus,target);
        }else{
            // this.setPic(vibrationPicStatus.openStatus,target);
        }
        this.vibrationStatus = !this.vibrationStatus;
    }


    //设置声音的初始状态
    static initVoicePic(target){
        if(settingMediator.voiceStatus){
            this.setPic(voicePicStatus.openStatus,target);
        }else{
            this.setPic(voicePicStatus.closeStatus,target);
        }
    }

    //设置震动的初始状态
    static initVibrationPic(target){
        if(settingMediator.vibrationStatus){
            this.setPic(vibrationPicStatus.openStatus,target);
        }else{
            this.setPic(vibrationPicStatus.closeStatus,target);
        }
    }

    //设置图片
    static setPic(curStatus,target){
        switch (curStatus) {
            case voicePicStatus.openStatus:
                this.loadShowPic(voicePicUrl[0],target);
                break;
            case voicePicStatus.closeStatus:
                this.loadShowPic(voicePicUrl[1],target);
                break;
            case vibrationPicStatus.openStatus:
                this.loadShowPic(vibrationPicUrl[0],target);
                break;
            case vibrationPicStatus.closeStatus:
                this.loadShowPic(vibrationPicUrl[1],target);
                break;
        }
    }

    //加载图片
    static loadShowPic(url : string ,target : cc.Sprite){
        cc.loader.loadRes(mainUrl + url,cc.SpriteFrame,(error, resource) => {
            if(error){
                console.log(error);
                return;
            }
            target.spriteFrame = resource;
        })
    }

    //调用震动
    static doVibration(type?){
        if(!settingMediator.vibrationStatus){
            return
        }

        if(type == null){
            type = 1;
        }

        if(cc.sys.os == "Windows"){
            if(type == 1){
                console.log("调用1000毫秒震动");
            }else{
                console.log("调用间隔震动");
            }
        }else{
            if(type == 1){
                jsb.reflection.callStaticMethod(androidVibratorUrl,"playVibrator","(I)V",1000);
            }else{
                jsb.reflection.callStaticMethod(androidVibratorUrl,"playIntervalVibrator","()V");
            }
        }
    }

    //设置背景音乐的音量
    static  setBgmVolume(value){
        AudioManage.setBgmVolume(value);
    }


    //设置音效音乐的音量
    static  setVoiceVolume(value){
        AudioManage.setVoiceVolume(value);
    }

    static getBgmVolume(){
        return AudioManage.bgmVolume;
    }

    static getVoiceVolume(){
        return AudioManage.sfxVolume
    }

}
